package com.mes.web.controller.hpf;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.hpf.SaleReceivable;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.ListBack;
import com.mes.entity.yzl.Receivable;
import com.mes.service.hpf.SaleReceivableService;
import com.mes.web.controller.yzl.Sup;

@Controller
@RequestMapping("/hpf")
public class SaleReceivableController {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Resource
	public SaleReceivableService saleReceivableService;
	
	/**
	 * 查询应收列表
	 * @param condition
	 * @return
	 */
	@RequestMapping("/receivableList")
    public @ResponseBody ListBack receivableList(Condition condition){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ListBack listBack = new ListBack();
        List<SaleReceivable> saleReceivableList = new ArrayList<>();
        List<Receivable> receivableList = new ArrayList<>();
        //获取当前月第一天
        Calendar fc = Calendar.getInstance();
        fc.add(Calendar.MONTH, 0);
        fc.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(fc.getTime());
        //获取当前月最后一天
        Calendar lc = Calendar.getInstance();    
        lc.set(Calendar.DAY_OF_MONTH, lc.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(lc.getTime());
        if(condition.getBeginDate() == null || condition.getBeginDate()==""){
        	condition.setBeginDate(first);
        	condition.setEndDate(last);
        	saleReceivableList = saleReceivableService.receivablePageList(condition);
        	for (int i = 0; i < saleReceivableList.size(); i++) {
        		SaleReceivable saleReceivable = saleReceivableList.get(i);
        		float outAmount = 0;
        		float totalAmount = 0;
        		if(saleReceivable.getOutAmount()!=null&&!"".equals(saleReceivable.getOutAmount())){
        			outAmount = Float.parseFloat(saleReceivable.getOutAmount());//出库金额
        		}
        		if(saleReceivable.getTotalAmount()!=null&&!"".equals(saleReceivable.getTotalAmount())){
        			totalAmount = Float.parseFloat(saleReceivable.getTotalAmount());//收款金额
        		}
        		Receivable receivable1 = new Receivable();
        		receivable1.setDateMonth(fc.get(Calendar.YEAR)+"-"+(fc.get(Calendar.MONTH)+1));
        		receivable1.setCustomerId(saleReceivable.getCid());
        		Receivable receivable2 = saleReceivableService.findByDateMonth(receivable1);
        		if(receivable2==null){
        			Receivable receivable3 = new Receivable();
        			receivable3.setDateMonth(fc.get(Calendar.YEAR)+"-"+(fc.get(Calendar.MONTH)));
            		receivable3.setCustomerId(saleReceivable.getCid());
            		Receivable receivable4 = saleReceivableService.findByDateMonth(receivable3);
            		Receivable receivable5 = new Receivable();
            		if(saleReceivable.getOutAmount()!=null&&!"".equals(saleReceivable.getOutAmount())){
            			receivable5.setOutAmount(outAmount);
            			receivable5.setMonthCollectP(totalAmount);
                		receivable5.setMonthCollectionP(outAmount-totalAmount);
                		if(receivable4!=null){
                			receivable5.setLastMonthP(receivable4.getClosingBalanceP());
                			receivable5.setClosingBalanceP(receivable4.getClosingBalanceP()+outAmount-totalAmount);
                		}else{
                			receivable5.setLastMonthP(0);
                			receivable5.setClosingBalanceP(outAmount-totalAmount);
                		}
            		}else{
            			receivable5.setOutAmount(0);
            			receivable5.setMonthCollectP(0);
                		receivable5.setMonthCollectionP(0);
                		if(receivable4!=null){
                			receivable5.setLastMonthP(receivable4.getClosingBalanceP());
                			receivable5.setClosingBalanceP(receivable4.getClosingBalanceP());
                		}else{
                			receivable5.setLastMonthP(0);
                			receivable5.setClosingBalanceP(0);
                		}
            		}
            		receivable5.setCustomerId(saleReceivable.getCid());
            		receivable5.setDateMonth(fc.get(Calendar.YEAR)+"-"+(fc.get(Calendar.MONTH)+1));
            		int isSuccess = saleReceivableService.addReceivable(receivable5);
        		}else{
        			receivable2.setOutAmount(outAmount);
        			receivable2.setMonthCollectP(totalAmount);
        			receivable2.setMonthCollectionP(outAmount-totalAmount);
        			receivable2.setClosingBalanceP(receivable2.getLastMonthP()+outAmount-totalAmount);
        			int isSuccess = saleReceivableService.resetReceivable(receivable2);
        		}
        	}
        }else{
        	int year = Integer.parseInt(condition.getBeginDate().split("-")[0]);
            int month = Integer.parseInt(condition.getBeginDate().split("-")[1]);
            Calendar zc = Calendar.getInstance();
            zc.set(Calendar.YEAR, year);
            zc.set(Calendar.MONTH, month - 1);
            zc.set(Calendar.DATE, 1);
            zc.roll(Calendar.DATE, -1);
            int maxDate = zc.get(Calendar.DATE);
            condition.setBeginDate(year+"-"+month+"-1");
        	condition.setEndDate(year+"-"+month+"-"+maxDate);
        	saleReceivableList = saleReceivableService.receivablePageList(condition);
        	for (int i = 0; i < saleReceivableList.size(); i++) {
        		SaleReceivable saleReceivable = saleReceivableList.get(i);
        		float outAmount = 0;
        		float totalAmount = 0;
        		if(saleReceivable.getOutAmount()!=null&&!"".equals(saleReceivable.getOutAmount())){
        			outAmount = Float.parseFloat(saleReceivable.getOutAmount());//出库金额
        		}
        		if(saleReceivable.getTotalAmount()!=null&&!"".equals(saleReceivable.getTotalAmount())){
        			totalAmount = Float.parseFloat(saleReceivable.getTotalAmount());//收款金额
        		}
        		Receivable receivable1 = new Receivable();
        		receivable1.setDateMonth(year+"-"+month);
        		receivable1.setCustomerId(saleReceivable.getCid());
        		Receivable receivable2 = saleReceivableService.findByDateMonth(receivable1);
        		if(receivable2==null){
        			Receivable receivable3 = new Receivable();
        			receivable3.setDateMonth(year+"-"+(month-1));
            		receivable3.setCustomerId(saleReceivable.getCid());
            		Receivable receivable4 = saleReceivableService.findByDateMonth(receivable3);
            		Receivable receivable5 = new Receivable();
            		if(saleReceivable.getOutAmount()!=null&&!"".equals(saleReceivable.getOutAmount())){
            			receivable5.setOutAmount(outAmount);
            			receivable5.setMonthCollectP(totalAmount);
                		receivable5.setMonthCollectionP(outAmount-totalAmount);
                		if(receivable4!=null){
                			receivable5.setLastMonthP(receivable4.getClosingBalanceP());
                			receivable5.setClosingBalanceP(receivable4.getClosingBalanceP()+outAmount-totalAmount);
                		}else{
                			receivable5.setLastMonthP(0);
                			receivable5.setClosingBalanceP(outAmount-totalAmount);
                		}
            		}else{
            			receivable5.setOutAmount(0);
            			receivable5.setMonthCollectP(0);
                		receivable5.setMonthCollectionP(0);
                		if(receivable4!=null){
                			receivable5.setLastMonthP(receivable4.getClosingBalanceP());
                			receivable5.setClosingBalanceP(receivable4.getClosingBalanceP());
                		}else{
                			receivable5.setLastMonthP(0);
                			receivable5.setClosingBalanceP(0);
                		}
            		}
            		receivable5.setCustomerId(saleReceivable.getCid());
            		receivable5.setDateMonth(year+"-"+month);
            		int isSuccess = saleReceivableService.addReceivable(receivable5);
        		}
        	}
        }
        condition.setBeginDate(condition.getBeginDate().split("-")[0]+"-"+Integer.parseInt(condition.getBeginDate().split("-")[1]));
        int count = saleReceivableService.receivableAllCount(condition);
        listBack.setTotal(count);
        receivableList = saleReceivableService.receivableAllPageList(condition);
        listBack.setRows(receivableList);
        return listBack;
    }
	
}
